from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

# 创建SQLAlchemy实例
db = SQLAlchemy()

class Ticket(db.Model):
    __tablename__ = 'tickets'
    
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    ticket_type = db.Column(db.String(20), nullable=False)  # 咨询/故障/反馈
    title = db.Column(db.String(200), nullable=False)
    description = db.Column(db.Text, nullable=False)
    status = db.Column(db.String(20), default='新建', nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    deleted_at = db.Column(db.DateTime, nullable=True)  # 软删除时间戳
    
    def to_dict(self):
        return {
            'id': self.id,
            'ticket_type': self.ticket_type,
            'title': self.title,
            'description': self.description,
            'status': self.status,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None
        }